<!doctype html>
<html>
<head>
	<meta charset="utf-8"/>
	<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0"><meta name="format-detection" content="telephone=no">
	<title>shake</title>
	<link rel="stylesheet" href="../plugin/seedsui/seedsui.min.css">
</head>

<body>
	<section id="page_shake" class="active">
		<header>
		  <div class="titlebar">
		    <a href="javascript:back()">
	            <i class="icon icon-arrowleft"></i>
	        </a>
		    <h1>摇一摇</h1>
		  </div>
		</header>
		<article>
			<div class="card padding8" id="shakecard">用力摇一摇你手机</div>
		</article>
	</section>
	<script src="../plugin/jquery/jquery.min.js"></script>
    <script src="../plugin/seedsui/seedsui.min.js"></script>
    <!--Exmobi能力-->
    <script src="../plugin/exmobi/exmobi.js"></script>
    <!--禁止ios弹性-->
    <script src="../plugin/inobounce/inobounce.min.js"></script>
	<script>
	(function(window,document,undefined){
		window.Shake=function(params){
			/*=========================
	          Params
	          ===========================*/
			var defaults={
				"threshold":3000,
				/*callbacks
				onShook:function(Slider)
				*/
			}
			params=params||{};
			for(var def in defaults){
				if(params[def]===undefined){
					params[def]=defaults[def];
				}
			}
			//Shake
			var s=this;

			s.speed=0;

			var last_update = 0;  
			var x = y = z = lastX = lastY = lastZ = 0;
			/*=========================
	          Method
	          ===========================*/
			function deviceMotionHandler(e) {
				s.target=e.target;
				var acceleration = e.accelerationIncludingGravity;
				var curTime = new Date().getTime();
				if((curTime - last_update) > 100) {  
					var diffTime = curTime - last_update;  
					last_update = curTime;  
					x = acceleration.x;
					y = acceleration.y;
					z = acceleration.z;
					s.speed = Math.abs(x + y + z - lastX - lastY - lastZ) / diffTime * 10000;
					if (s.speed > s.params.threshold) {
						//CallBack
						if(s.params.onShook)onShook(s);
					}
					lastX = x;
					lastY = y;
					lastZ = z;
				}
			}
			if (window.DeviceMotionEvent){  
				window.addEventListener('devicemotion',deviceMotionHandler,false);
			}else{
				if(s.params.onFail)s.params.onError("您的手机不支持重力感应事件");
			}
		}
	})(window,document,undefined);
	
	window.onload=function(){
		var sk=new Shake({
			"onShook":function(e){
				document.getElementById("shakecard").innerHTML="摇动了";
			},
			"onFail":function(msg){
				console.log(msg);
			}
		});
	}
	//定义exmobi返回
	function back(){history.go(-1);}
	</script>
</body>
</html>
